package com.yswy.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yswy.app.service.UserLoginLogService;
import com.yswy.domain.entity.UserLoginLog;
import com.yswy.domain.mapper.UserLoginLogMapper;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;


@Slf4j
@Service
public class UserLoginLogServiceImpl extends ServiceImpl<UserLoginLogMapper, UserLoginLog>
        implements UserLoginLogService {


    @Override
    public void addUserLoginLog(Long userId, String channle, String version, String ip) {
        Integer count = count(new LambdaQueryWrapper<UserLoginLog>().eq(UserLoginLog::getUserId ,userId)
                .last("and to_days(now()) - to_days(create_time) = 0"));
        if (count<=0){
            save(UserLoginLog.builder()
                    .createTime(new Date())
                    .channle(channle)
                    .version(version)
                    .loginIp(ip)
                    .userId(userId)
                    .build());
        }
    }

    @Override
    public Integer countByUserId(Long userId ) {
        return  count(new LambdaQueryWrapper<UserLoginLog>().eq(UserLoginLog::getUserId ,userId));
    }
}
